<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>

  <form action="/user/62eae3535ac1a70155b896b3" method="post" enctype="multipart/form-data">
    <input type="text" id="ipUsername" name="username"><br>
    <input type="text" id="ipPassword" name="password"><br>
    <img src="/img/default.jpg" alt="" id="imgAvatar" style="width:100px;"><br>
    <input type="file" name="avitar" /><br />
    <button id="btn">提交</button>
  </form>

  <script src="/js/ajax.js"></script>

  <script>
    function showInfo() {
      ajaxPromise({
        url: "/user/62eae3535ac1a70155b896b3",
        headers: {
          "authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MmVhZTM1MzVhYzFhNzAxNTViODk2YjMiLCJ1c2VybmFtZSI6ImhlaWdlIiwicGFzc3dvcmQiOiIxMjM0NTYiLCJpYXQiOjE2NTk5MDA2OTQsImV4cCI6MTY2MDUwNTQ5NH0.F_aZHe-wG-kcOvfQjBwzCcml61k-iFG4Q0aCI1csMNg"
        }
      }).then(
        data => {
          console.log("data=", data)
          const { username, password, avatar } = JSON.parse(data)
          ipUsername.value = username
          ipPassword.value = password
          imgAvatar.src = avatar
        }
      ).catch(
        err => console.log("err=", err)
      )
    }
  </script>

  <script>
    showInfo()
    btn.onclick = async function (e) {
      // console.log("ipAvatar.value=", ipAvatar.value);
      // try {
      //   const result = await ajaxPromise({
      //     url: "/user/62eae3535ac1a70155b896b3",
      //     method: "PUT",
      //     headers: {
      //       "authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MmVhZTM1MzVhYzFhNzAxNTViODk2YjMiLCJ1c2VybmFtZSI6ImhlaWdlIiwicGFzc3dvcmQiOiIxMjM0NTYiLCJpYXQiOjE2NTk5MDA2OTQsImV4cCI6MTY2MDUwNTQ5NH0.F_aZHe-wG-kcOvfQjBwzCcml61k-iFG4Q0aCI1csMNg"
      //     },
      //     data: {
      //       username: ipUsername.value,
      //       password: ipPassword.value,
      //       avatar: ipAvatar.value
      //     },
      //     dataType: "json"
      //   })

      //   console.log("update result", result);
      //   showInfo()
      // } catch (err) {
      //   console.log("update err", err);
      // }

      setTimeout(() => {
        showInfo()
      }, 1000);
    }
  </script>

</body>

</html>